package com.peas.platform.common.core.configuration;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.lang.Assert;
import com.peas.platform.common.core.enums.BaseEnum;
import lombok.SneakyThrows;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public enum DateFormatReg implements BaseEnum {

    REG_DATE_FMT_Y("REG_DATE_FMT_Y", "^\\d{4}$"),
    REG_DATE_FMT_Y_M("REG_DATE_FMT_Y_M", "^\\d{4}-\\d{1,2}$"),
    REG_DATE_FMT_Y_M_D("REG_DATE_FMT_Y_M_D", "^\\d{4}-\\d{1,2}-\\d{1,2}$"),
    REG_DATE_FMT_Y_M_D_H("REG_DATE_FMT_Y_M_D_H", "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}$"),
    REG_DATE_FMT_Y_M_D_HM("REG_DATE_FMT_Y_M_D_HM", "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}$"),
    REG_DATE_FMT_Y_M_D_HMS("REG_DATE_FMT_Y_M_D_HMS", "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$"),
    REG_DATE_FMT_Y_M_D_HMSSS("REG_DATE_FMT_Y_M_D_HMSSS", "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{1,3}$"),
    ;

    private final String key;
    private final String value;

    DateFormatReg(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public String getValue() {
        return value;
    }

    public static boolean checkFormat(String text) {
        if (text == null) {
            return true;
        }
        return text.matches(DateFormatReg.REG_DATE_FMT_Y.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_H.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HM.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HMS.getValue())
                || text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HMSSS.getValue());
    }

    @SneakyThrows
    public static String getPattern(String text) {
        if (text == null) {
            return null;
        }
        if (text.matches(DateFormatReg.REG_DATE_FMT_Y.getValue())) {
            return DatePattern.NORM_YEAR_PATTERN;
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M.getValue())) {
            return DatePattern.NORM_MONTH_PATTERN;
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D.getValue())) {
            return DatePattern.NORM_DATE_PATTERN;
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_H.getValue())) {
            return "yyyy-MM-dd HH";
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HM.getValue())) {
            return DatePattern.NORM_DATETIME_MINUTE_PATTERN;
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HMS.getValue())) {
            return DatePattern.NORM_DATETIME_PATTERN;
        } else if (text.matches(DateFormatReg.REG_DATE_FMT_Y_M_D_HMSSS.getValue())) {
            return DatePattern.NORM_DATETIME_MS_PATTERN;
        } else {
            throw new ParseException("Invalid Date value '" + text + "'", 0);
        }
    }

    public static DateFormat getDateFormat(String pattern) {
        Assert.notBlank(pattern, "pattern is blank");
        return new SimpleDateFormat(pattern, Locale.CHINA);
    }

}
